package org.aaron.demos.cp;

import java.util.HashMap;
import java.util.Map;

import org.aaron.demos.cp.FirstProviderMetaData.UserTableMetaData;
import org.aaron.demos.db.SqliteDatabaseHelper;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

public class FirstContentProvider extends ContentProvider {
	private static final UriMatcher sUriMacher;
	private static final int INCOME_USER_COLLECTION = 1;
	private static final int INCOME_USER_ITEM = 2;

	static {
		sUriMacher = new UriMatcher(UriMatcher.NO_MATCH);
		sUriMacher.addURI(FirstProviderMetaData.AUTHORLY, "users", INCOME_USER_COLLECTION);
		sUriMacher.addURI(FirstProviderMetaData.AUTHORLY, "users/#", INCOME_USER_ITEM);
	}

	private static Map<String, String> userAliasMap;
	static {
		userAliasMap = new HashMap<String, String>();
		userAliasMap.put(UserTableMetaData._ID, UserTableMetaData._ID);
		userAliasMap.put(UserTableMetaData.USER_NAME, UserTableMetaData.USER_NAME);
	}

	private SqliteDatabaseHelper databaseHelper;

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		return 0;
	}

	// 根据传入的Uri，返回该Uri所表示的数据类型
	@Override
	public String getType(Uri uri) {
		Log.d("Debug", "getType method.");
		switch (sUriMacher.match(uri)) {
		case INCOME_USER_COLLECTION:
			return UserTableMetaData.CONTENT_TYPE;
		case INCOME_USER_ITEM:
			return UserTableMetaData.CONTENT_TYPE_ITEM;
		default:
			throw new IllegalArgumentException("ERROR URI:" + uri);
		}
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		Log.d("FirstContentProvider", "insert");
		SQLiteDatabase sqLiteDatabase = databaseHelper.getWritableDatabase();
		long rowId = sqLiteDatabase.insert(UserTableMetaData.TABLE_NAME, null, values);
		if (rowId > 0) {
			Uri insertedUserUri = ContentUris.withAppendedId(uri, rowId);
			getContext().getContentResolver().notifyChange(insertedUserUri, null);
			return insertedUserUri;
		}
		throw new SQLException("Fail insert row into " + uri);
	}

	@Override
	public boolean onCreate() {
		Log.d("FirstContentProvider", "onCreate");
		databaseHelper = new SqliteDatabaseHelper(getContext(), FirstProviderMetaData.DB_NAME);
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
		// Sqlite查询语句构造器
		SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
		Log.d("URI_MATCHER ", String.valueOf(sUriMacher.match(uri)));
		switch (sUriMacher.match(uri)) {
		case INCOME_USER_COLLECTION:
			queryBuilder.setTables(UserTableMetaData.TABLE_NAME);
			queryBuilder.setProjectionMap(userAliasMap);
			break;
		case INCOME_USER_ITEM:
			queryBuilder.setTables(UserTableMetaData.TABLE_NAME);
			queryBuilder.setProjectionMap(userAliasMap);
			queryBuilder.appendWhere(UserTableMetaData._ID + "=" + uri.getPathSegments().get(1));
			break;
		}
		if(TextUtils.isEmpty(sortOrder)){
			sortOrder=UserTableMetaData.DEFAULT_SORT_ORDER;
		}
		SQLiteDatabase sqLiteDatabase = databaseHelper.getWritableDatabase();
		Cursor cursor=queryBuilder.query(sqLiteDatabase, projection, selection, selectionArgs, null, null, sortOrder);
		cursor.setNotificationUri(getContext().getContentResolver(), uri);	
		return cursor;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
		return 0;
	}

}
